package info.flexination.actionscripts
{
    // imports needed for this AS Class
    import flash.events.NetStatusEvent;
    import flash.system.System;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import mx.managers.CursorManager;
    import flash.events.HTTPStatusEvent;
    import flash.events.IOErrorEvent;
    import mx.core.Application;

    public class ConnectionStatus
    {
        // embed images and declare persistent image classes
        [Embed(source="../../../assets/images/smile.png")]
        [Bindable] public var imgConnected:Class;
        [Embed(source="../../../assets/images/frown.png")]
        [Bindable] public var imgDisconnected:Class;
        
        // checks the connectivity by accessing the URL, http://labs.insideflex.com
        public function checkConnection():void {
            // triggers the display of a watch glass or busy cursor
            CursorManager.setBusyCursor();
            // declare a URLRequest variable
            var headRequest:URLRequest = new URLRequest();
            // set the URLRequest method to HEAD
            headRequest.method = "HEAD";
            // set the URL
            headRequest.url = "http://labs.insideflex.com";
            // declare a URLLoader variable and pass the URLRequest
            var response:URLLoader = new URLLoader(headRequest);
            /* add a HTTP_STATUS event listener: Listens for the HTTP_STATUS; 
            statusChanged() function is called when the listener event occurs */
            response.addEventListener(HTTPStatusEvent.HTTP_STATUS, statusChanged);
            /* add a IO_ERROR event listener: Listens for an I/O connectivity issue; 
            onError() function is called if the listener event occurs */
            response.addEventListener(IOErrorEvent.IO_ERROR, onError); 
        }

        // handler function for networkChangeEvent events
        public function onConnectionChange(networkChangeEvent:Event):void {
            // check the connection
            checkConnection();
        }
        // handler function for HTTPStatusEvent events
        private function statusChanged(status:HTTPStatusEvent):void {
            // remove the watch glass or busy cursor
            CursorManager.removeBusyCursor();
            // conditional to check the status
            if (status.status == 0) {
                // change the image, imgStatus, to the frown.png
                mx.core.Application.application.imgStatus.source = imgDisconnected;
                // change the toolTip for the image, imgStatus
                mx.core.Application.application.imgStatus.toolTip = "Offline and not connected to the Internet...";
                // toggle buttons' availability to unavailable
                mx.core.Application.application.btnSearch.enabled = false;
                mx.core.Application.application.blnConnected = false;
                mx.core.Application.application.btnViewSource.enabled = false;
            } else {
                // change the image, imgStatus, to the smile.png
                mx.core.Application.application.imgStatus.source = imgConnected;
                // change the toolTip for the image, imgStatus
                mx.core.Application.application.imgStatus.toolTip = "Online and connected to the Internet...";
                // toggle buttons' availability to available
                mx.core.Application.application.btnSearch.enabled = true;
                mx.core.Application.application.blnConnected = true;
                mx.core.Application.application.btnViewSource.enabled = true;
            }
        }
        // handler function for IOErrorEvent events
        private function onError(error:IOErrorEvent):void {
            // remove the watch glass or busy cursor
            CursorManager.removeBusyCursor();
            // change the image, imgStatus, to the frown.png
            mx.core.Application.application.imgStatus.source = imgDisconnected;
                // change the toolTip for the image, imgStatus
            mx.core.Application.application.imgStatus.toolTip = "Offline and not connected to the Internet...";
            // toggle buttons' availability to unavailable
            mx.core.Application.application.btnSearch.enabled = false;
            mx.core.Application.application.blnConnected = false;
        }
    }
    
}